﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.OA
{
    public partial class _msgedit : PageBase
    {
        public string us,sid;
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            if (!IsPostBack)
            {              
                txt.OnClientTrigger2Click = Window1.GetSaveStateReference(hidtxt.ClientID,hidval.ClientID) +
                Window1.GetShowReference("../function/getuser.aspx", "选择收件人");

                if (!String.IsNullOrEmpty(Request["sid"]))
                {
                     sid= Request["sid"].Trim();
                     hidsid.Text = Request["sid"].Trim();  
                     fill();
                }
                
            }
                     
        }

        void fill()
        {
            DataTable ds = BLL.msg.msgbase.get_list(sid);
            if (ds.Rows.Count > 0)
            {
                txt.Text = ds.Rows[0]["outnm"].ToString(); //收件人
                txttitel.Text = ds.Rows[0]["title"].ToString(); //标题
                FCKeditor1.Value = HttpUtility.HtmlDecode(ds.Rows[0]["contont"].ToString()); //信息内容
                hidstype.Text = ds.Rows[0]["stype"].ToString();
                hidimg.Text = ds.Rows[0]["sfile"].ToString();
                hidowen.Text = ds.Rows[0]["owenid"].ToString();
            }
        }


        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;          
        }

   
        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            txt.Text = hidtxt.Text;
          
            string key = txt.Text;
            //  stype = hidstype.Text;
            try
            {
             //   if (key == "") whe = BLL.pub.pubwhere.get_where_sql(Session["ID"].ToString(), "addbook");
               // BindGrid();
            }
            catch (Exception ex)
            {
                Alert.Show("查询条件错误");
            }
            finally
            {
              //  whe = "";
            }
        }

        protected void btsave_Click(object sender, EventArgs e)
        {
         
            string rename = hidtxt.Text; //收件人

            string title = txttitel.Text; //主题           

          
            string day = DateTime.Now.ToString(); //发送日期
            string memo = HttpUtility.HtmlEncode(FCKeditor1.Value); //信息内容
            string path =hidimg.Text;
            string owen="";
            string person=us;

            string fileName = fileup.ShortFileName; //附件名
            if (fileName.Length > 0)
            {
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                path = "~/file/msg/" + fileName;
                if (fileup.HasFile)
                    fileup.SaveAs(Server.MapPath(path));
            }

            BLL.msg.msgbase.DeleteList2(hidsid.Text);  //删除原记录
            BLL.msg.msgbase.ins_msg(owen, person, rename.ToString(), title, memo, path, "0"); //发送箱

            string[] rstr = rename.Split(',');
            if (rstr.Length != 0)
            {

                for (int i = 0; i < rstr.Length; i++)
                {
                    BLL.msg.msgbase.ins_msg(owen, person, rstr[i].ToString(), title, memo, path, "1");//收件箱
                }
            }

            Alert.Show("完成 ");
        }


        //保存草稿
        protected void btsave2_Click(object sender, EventArgs e)
        {
            string owen = hidowen.Text;
            string rename = hidtxt.Text; //收件人

            string title = txttitel.Text; //主题           

            string person = us; //发送人
            string day = DateTime.Now.ToString(); //发送日期
            string memo = HttpUtility.HtmlEncode(FCKeditor1.Value); //信息内容
            string path = hidimg.Text;
          

            string fileName = fileup.ShortFileName; //附件名
            if (fileName.Length > 0)
            {
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                path = "~/file/msg/" + fileName;
                if (fileup.HasFile)
                    fileup.SaveAs(Server.MapPath(path));
             }

            BLL.msg.msgbase.DeleteList2(hidsid.Text);  //删除原记录
            BLL.msg.msgbase.ins_msg(owen, person, rename.ToString(), title, memo, path, "2"); //草稿
         
            //string[] rstr = rename.Split(',');
            //if (rstr.Length!= 0)
            //{

            //    for (int i = 0; i < rstr.Length; i++)
            //    {
            //        BLL.msg.msgbase.ins_msg(owen, person, rstr[i].ToString(), title, memo, path, "1");
            //    }
            //}

            Alert.Show("完成 ");
        }


    }
}